package com.bw.goods_module.bind;

import android.view.LayoutInflater;
import android.view.View;

import androidx.databinding.BindingAdapter;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.bw.goods_module.R;
import com.bw.goods_module.home.GoodsEntity;
import com.bw.goods_module.home.HomeBannerEntity;
import com.bw.goods_module.home.HomeMenuEntity;
import com.bw.goods_module.home.adapter.GoodsAdapter;
import com.bw.goods_module.home.adapter.HomeRecommandAdapter;
import com.bw.goods_module.home.adapter.HotAdapter;
import com.bw.goods_module.home.adapter.MenuAdapter;
import com.bw.goods_module.home.adapter.TextBannerAdapter;
import com.bw.goods_module.home.adapter.TopBannerAdapter;
import com.bw.widget_sdk.decoration.HomeGoodsDecoration;
import com.bw.widget_sdk.decoration.HomeHotDecoration;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.youth.banner.Banner;
import com.youth.banner.transformer.ZoomOutPageTransformer;

import java.util.List;

//商城首页Databinding自定义属性
public class HomeBind {

    /**
     * 由于HomeGoods为接口返回数据:数据分页，数据刷新，数据为动态更新
     * ObservableField.set()->bindHomeRv会接收新传递过来的数据
     * bindHomeRv添加对应逻辑处理，防止recyclerview反复关联适配器
     * */
    @BindingAdapter(value ={"homegoods","homebanner","txtbanner","homemenu",
            "homehot","homevp","transformer","homeitemclick"})
    public static void bindHomeRv(RecyclerView recyclerView,
                                  List<GoodsEntity> homegoods, List<HomeBannerEntity>homebanner,
                                  List<String> txtbanner, List<HomeMenuEntity> homemenu,
                                  List<String>homehot, List<HomeBannerEntity>homevp,
                                  ViewPager.PageTransformer transformer,
                                  OnItemClickListener homeitemclick){
        //获取商品recyclerview关联的适配器
        GoodsAdapter goodsAdapter = (GoodsAdapter) recyclerView.getAdapter();
        if (goodsAdapter == null){
            //适配器为空->第一次进入界面数据绑定
            //创建商品列表适配器对象
            goodsAdapter = new GoodsAdapter();
            goodsAdapter.setOnItemClickListener(homeitemclick);
            //获取万能适配器中要显示使用的头布局
            View headView = LayoutInflater.from(recyclerView.getContext())
                    .inflate(R.layout.view_home_banner,null,false);
            Banner home_view_banner = headView.findViewById(R.id.home_view_banner);
            Banner home_text_banner = headView.findViewById(R.id.home_text_banner);
            RecyclerView home_menu_rv = headView.findViewById(R.id.home_menu_rv);
            RecyclerView home_hot_rv = headView.findViewById(R.id.home_hot_rv);
            ViewPager home_vp = headView.findViewById(R.id.home_vp);
            home_vp.setOffscreenPageLimit(4);
            home_vp.setPageTransformer(true,transformer);
            //homebanner配置
            home_view_banner.setAdapter(new TopBannerAdapter(homebanner))
                    .setOrientation(Banner.HORIZONTAL)
                    .start();
            //文字公告配置
            home_text_banner.setAdapter(new TextBannerAdapter(txtbanner))
                    .setOrientation(Banner.VERTICAL)
                    .setPageTransformer(new ZoomOutPageTransformer())
                    .start();
            //设置菜单列表
            GridLayoutManager menuManager = new GridLayoutManager(recyclerView.getContext(),4);
            menuManager.setOrientation(GridLayoutManager.VERTICAL);
            home_menu_rv.setLayoutManager(menuManager);
            MenuAdapter menuAdapter = new MenuAdapter();
            menuAdapter.setOnItemClickListener(homeitemclick);
            menuAdapter.setNewInstance(homemenu);
            home_menu_rv.setAdapter(menuAdapter);
            //处理热门商品
            LinearLayoutManager hotManager = new LinearLayoutManager(recyclerView.getContext());
            hotManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            home_hot_rv.setLayoutManager(hotManager);
            HotAdapter hotAdapter = new HotAdapter();
            home_hot_rv.addItemDecoration(new HomeHotDecoration(home_hot_rv.getContext()));
            home_hot_rv.setAdapter(hotAdapter);
            hotAdapter.setNewInstance(homehot);
            //处理推荐ViewPager
            home_vp.setAdapter(new HomeRecommandAdapter(homevp));
            //处理商品列表
            goodsAdapter.addHeaderView(headView);
            GridLayoutManager goodsManager = new GridLayoutManager(recyclerView.getContext(),2);
            goodsManager.setOrientation(GridLayoutManager.VERTICAL);
            recyclerView.setLayoutManager(goodsManager);
            recyclerView.addItemDecoration(new HomeGoodsDecoration(recyclerView.getContext()));
            recyclerView.setAdapter(goodsAdapter);
            goodsAdapter.setNewInstance(homegoods);
        }else {
            //适配器不为空->用户页面逻辑进入到了上拉加载或者下拉刷新获取新数据源逻辑,不需要重新绑定头布局
            //更新数据源
            goodsAdapter.setNewInstance(homegoods);
        }
    }


    /**
     * 添加SmartRefreshLayout监听，数据分页处理
     * */
//    public static void bindHomeSmart

}
